package com.woniuxy.threeproject.end_water_project_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.threeproject.end_water_project_user.dao.EmployeeMapper;
import com.woniuxy.threeproject.end_water_project_user.dao.UserMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Employee;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.threeproject.end_water_project_user.commons.LateFeeCal;
import com.woniuxy.threeproject.end_water_project_user.dao.*;
import com.woniuxy.threeproject.end_water_project_user.domain.*;
import com.woniuxy.threeproject.end_water_project_user.dao.EmployeeMapper;
import com.woniuxy.threeproject.end_water_project_user.dao.UserMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Employee;
import com.woniuxy.threeproject.end_water_project_user.domain.PaymentForm;
import com.woniuxy.threeproject.end_water_project_user.dao.PaymentFormMapper;
import com.woniuxy.threeproject.end_water_project_user.domain.User;
import com.woniuxy.threeproject.end_water_project_user.domain.form.billform.BillDetailForm;
import com.woniuxy.threeproject.end_water_project_user.domain.form.billform.GetBillForm;
import com.woniuxy.threeproject.end_water_project_user.exception.EndWaterRuntimeException;
import com.woniuxy.threeproject.end_water_project_user.exception.EndWaterRuntimeException;
import com.woniuxy.threeproject.end_water_project_user.domain.form.billform.BillDetailForm;
import com.woniuxy.threeproject.end_water_project_user.domain.form.billform.GetBillForm;
import com.woniuxy.threeproject.end_water_project_user.exception.EndWaterRuntimeException;
import com.woniuxy.threeproject.end_water_project_user.service.IPaymentFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.threeproject.end_water_project_user.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

import javax.annotation.Resource;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 缴费表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
@Slf4j
public class PaymentFormServiceImpl extends ServiceImpl<PaymentFormMapper, PaymentForm> implements IPaymentFormService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private PaymentFormMapper paymentFormMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private MeterreadingMapper meterreadingMapper;
    @Resource
    private PaymentDetailMapper paymentDetailMapper;


    //根据卡号查询用户信息
    @Override
    public User getUserByCard(String card){
        log.info("根据卡号查询用户信息 {}",card);
        //通过户号查询用户姓名
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card",card);
        List<User> users = userMapper.selectList(queryWrapper);
        if(users.size()==0){
            //查询不到此用户则抛异常
            log.info("用户不存在");
            throw new EndWaterRuntimeException("用户不存在");

        }
        return users.get(0);
    }

    /*
    获取电子发票详情
     */
    @Override
    public BillDetailForm getBillDetailForm(GetBillForm getBillForm) {
        log.info("执行查询发票详情,{}",getBillForm);
        User user = getUserByCard(getBillForm.getCard());
        BillDetailForm detailForm = new BillDetailForm();
        detailForm.setCard(getBillForm.getCard());
        detailForm.setUserName(user.getUserName());
        //通过发票号获取发票信息
        QueryWrapper<PaymentForm> paymentFormQueryWrapper = new QueryWrapper<>();
        paymentFormQueryWrapper.eq("bill_num",getBillForm.getBillNum());
        List<PaymentForm> paymentForms = paymentFormMapper.selectList(paymentFormQueryWrapper);
        if(paymentForms.size()==0){
            log.info("========");
            throw new EndWaterRuntimeException("此发票号不存在");
        }
        detailForm.setBillNum(getBillForm.getBillNum());
        detailForm.setBillTime(paymentForms.get(0).getBillTime());
        detailForm.setBillMoney(paymentForms.get(0).getBillMoney());
        detailForm.setPayWay(paymentForms.get(0).getPayWay());
        //获取员工信息
        Employee employee = employeeMapper.selectById(paymentForms.get(0).getEmployeeId());
        if(null==employee){
            throw new EndWaterRuntimeException("此员工不存在");
        }
        detailForm.setEmployeeName(employee.getEmployeeName());
        log.info(detailForm.toString());
        return detailForm;
    }

    @Override
    public int insert(PaymentForm p) {
        int i = paymentFormMapper.insert(p);
        if(i==0){
            throw new EndWaterRuntimeException("费用表更新出错了！");
        }
        return i;
    }

    @Override
    public PaymentForm getPayFormId(String fpuUid) {
        QueryWrapper<PaymentForm> qw = new QueryWrapper<>();
        qw.eq("bill_num",fpuUid);
        PaymentForm repay = paymentFormMapper.selectOne(qw);
        return  repay;
    }
    //添加缴费表
    @Override
    public void addPayForm(PaymentForm paymentForm,String card){
        log.info("添加缴费表，开始查询用户表 {}",paymentForm.getUserId());
        User user = getUserByCard(card);
        paymentForm.setUserId(user.getUserId());
        log.info("添加缴费表，开始执行添加 {}",paymentForm);
        int num = paymentFormMapper.insert(paymentForm);
        if (num == 0) {
            log.warn("添加失败?");
            throw new EndWaterRuntimeException("添加失败");
        }
        Integer paymentFormId = paymentForm.getPaymentFormId();
        QueryWrapper<Meterreading> qw = new QueryWrapper<>();
        qw.eq("water_meter_id",user.getWaterMeterId());
        List<Meterreading> meterreadings = meterreadingMapper.selectList(qw);
        if (meterreadings.size() == 0) {
            log.warn("没有需要缴纳的费用");
            throw new EndWaterRuntimeException("没有需要缴纳的费用");
        }
        log.info("添加缴费详情：开始执行！");
        for (Meterreading meterreading : meterreadings) {
            PaymentDetail paymentDetail = new PaymentDetail();
            paymentDetail.setWaterNum(meterreading.getWaterNum());
            paymentDetail.setPrice(meterreading.getPrice());
            paymentDetail.setReceivable(meterreading.getPrice()*meterreading.getWaterNum());
            Double lateFee = LateFeeCal.lateFeeNo(meterreading);
            paymentDetail.setLateFee(lateFee);
            paymentDetail.setPaymentFormId(paymentFormId);
            paymentDetail.setUserId(user.getUserId());
            paymentDetailMapper.insert(paymentDetail);
        }

    }
    //获取缴费表最后一条的信息
    @Override
    public Integer findEndPaymentForm(){
        log.info("查询缴费表最后一条：开始执行");
        Integer paymentFormID = paymentFormMapper.findEndPaymentForm();
        return paymentFormID;
    }
    //根据定单号修改订单状态和支付方式
    @Override
    public void updatePaymentForm(String outTradeNo,char payWay){
        log.info("缴费成功：修改订单状态和支付方式 {} {}",outTradeNo,payWay);
        UpdateWrapper<PaymentForm> uw = new UpdateWrapper<>();
        uw.set("pay_way",payWay);
        uw.set("state",1);
        uw.eq("bill_num",outTradeNo);
        paymentFormMapper.update(null,uw);
        log.info("修改用水表状态");
        QueryWrapper<PaymentForm> qw = new QueryWrapper<>();
        qw.eq("bill_num",outTradeNo);
        log.info("修改用水表状态=>查询缴费表");
        List<PaymentForm> paymentForms = paymentFormMapper.selectList(qw);
        Integer userId = paymentForms.get(0).getUserId();
        log.info("修改用水表状态=>查询缴费表=>查询用户表");
        User user = userMapper.selectById(userId);
        UpdateWrapper<Meterreading> uw1 = new UpdateWrapper<>();
        uw1.set("state",1);
        uw1.eq("water_meter_id",user.getWaterMeterId());
        meterreadingMapper.update(null,uw1);
    }
}
